#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-09-02 12:27
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：
from sqlalchemy import *

base = MetaData()

# app表
config_table = Table(
        "AppConfig",
        base,
        Column('AppId', Integer(), comment = '主键', server_default = '0', primary_key = True),
        Column('AppName', String(100), comment = '程序名称', server_default = 'APP'),
        Column('StartSdk', Integer(), comment = '启用sdk'),
        Column('StartS7', Integer(), comment = '启用s7'),
        Column('EnablePassword', Integer(), comment = '启用密码'),
        )

# 数据库配置表
connect_table = Table(
        "ConnectConfig",
        base,
        Column('id', Integer(), comment = '主键'),
        Column('name', String(50), comment = '名称'),
        Column('type', String(50), comment = '类型'),
        Column('mode', Integer, comment = '模式'),
        Column('host', String(20), comment = '主机'),
        Column('port', Integer, comment = '端口'),
        Column('username', String(50), comment = '用户'),
        Column('password', String(50), comment = '密码'),
        Column('database', String(50), comment = '库实例'),
        Column('dsn', String(50), comment = 'ODBC连接名称'),
        Column('mincached', Integer, comment = '池初始连接数'),
        Column('maxconnections', Integer, comment = '池最大连接数'),
        Column('databasepath', String(100), comment = '数据库路径'),
        Column('format', String(10), comment = '格式'),
        Column('left', Integer, comment = '在左边'),
        Column('right', Integer, comment = '在右边')

        )
# 字段表
field_table = Table(
        "FieldConfig",
        base,
        Column('Id', Integer(), comment = '序号'),
        Column('Description', String(50), comment = '注释'),
        Column('TableName', String(50), comment = '源表名称'),
        Column('TargetTable', String(50), comment = '目的表名称'),
        Column('FieldName', String(500), comment = '源字段'),
        Column('TargetField', String(50), comment = '目的字段'),
        Column('DefaultValue', String(50), comment = '默认值'),
        Column('Mode', Integer, comment = '模式'),
        Column('SerialNumber', String(10), comment = '序号'),
        )
# 日志配置表
log_table = Table(
        "LogConfig",
        base,
        Column('LogLevel', Integer, comment = '日志级别'),
        Column('LogSave', Integer, comment = '是否存储'),
        Column('LogSavePath', String(200), comment = '存储路径'),
        Column('LogSaveCycle', Integer, comment = '存储周期'),
        Column('LogSaveUnit', String(10), comment = '周期单位'),
        Column('LogSaveNumber', Integer, comment = '日志数量'),
        Column('LogSaveSize', Integer, comment = '日志大小'),

        )
# 任务配置表
scheduler_table = Table(
        "SchedulerConfig",
        base,
        Column('ID', Integer, comment = '主键'),
        Column('Description', String(200), comment = '注释'),
        Column('Name', String(200), comment = '任务名称'),
        Column('Cycle', Integer, comment = '调度周期'),
        Column('AutoStart', Integer, comment = '自启'),
        Column('SourceConnectName', String(50), comment = '源连接名称'),
        Column('TagConnectName', String(50), comment = '目的连接'),
        Column('DateField', String(50), comment = '源日期字段'),
        Column('SerialField', String(50), comment = '流水主键'),
        Column('MarkField', String(50), comment = '标记字段'),
        Column('MarkFieldInitValue', String(50), comment = '标记值'),
        Column('MarkFieldEndValue', String(50), comment = '标记更新值'),
        Column('Other', String(200), comment = '其它条件'),
        Column('Mode', Integer, comment = '模式'),
        Column('SerialNumber', Integer, comment = '序号'),

        )
# 串口配置表
serial_table = Table(
        "SerialConfig",
        base,
        Column('port', String(50), comment = '串口'),
        Column('baudrate', Integer, comment = '速率'),
        Column('bytesize', Integer, comment = '数据位'),
        Column('parity', String(10), comment = '校验位'),
        Column('stopbits', FLOAT(5), comment = '停止位'),
        Column('timeout', FLOAT(10), comment = '超时'),

        )
# s7配置表
s7_table = Table(
        "s7Config",
        base,
        Column('name', String(50), comment = '串口'),
        Column('host', String(20), comment = '速率'),
        Column('port', Integer, comment = '端口'),
        Column('rack', Integer, comment = '板卡'),
        Column('slot', Integer, comment = '插槽'),
        Column('tsap', String(10), comment = '通用电缆'),
        )
# modbus配置表
modbus_table = Table(
        "ModbusConfig",
        base,
        Column('name', String(50), comment = '名称'),
        Column('mode', String(50), comment = '模式'),
        Column('host', String(20), comment = '主机'),
        Column('port', Integer, comment = '端口'),
        Column('com', String(50), comment = '串口'),
        Column('baudrate', Integer, comment = '速率'),
        Column('bytesize', Integer, comment = '数据位'),
        Column('parity', String(10), comment = '校验位'),
        Column('stopbits', FLOAT(5), comment = '停止位'),
        Column('timeout', FLOAT(10), comment = '超时'),
        )
# 标记配置表
flag_table = Table(
        "LastMark",
        base,
        Column('Mark', String(50), comment = '最后标记'),
        Column('UpdateTime', String(50), comment = '更新时间'),
        Column('TaskName', String(50), comment = '任务名称'),

        )


# 初始化本地表
def init_config(local):
    base.create_all(local.engine)
    with local.get_session() as session:

        session.autocommit = True
        data = session.query(config_table).all()
        # 初始程序表
        if not data:
            ins = config_table.insert()
            val = ins.values(AppId = 1)
            session.execute(val)
        # 日志表
        data = session.query(log_table).all()
        if not data:
            ins = log_table.insert()
            val = ins.values([10, 1, './log', 1, 'D', 10, 5])
            session.execute(val)